#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compile_run;
using namespace httplib;


const std::string oj_server_ip = "127.0.0.1";
const std::string oj_server_port = "8080";

void manual(const std::string argv)
{
    std::cout << "Use Error,please Use:" << "\n";
    std::cout << "\t" << argv << " port" << "\n";
}
int main(int args,char* argv[])
{
    if(args != 2)
    {
        manual(argv[0]);
        exit(1);
    }
    Server svr;
    //编译服务的接口设置
    svr.Post("/compile_and_run",[](const Request& req,Response& res){
        LOG(INFO) << "有请求出现ip:" << req.remote_addr << " 端口:" << req.remote_port << "\n";
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            compile_run::Star(in_json,&out_json);
            res.set_content(out_json,"application/json;charset=utf-8");
        }
    });
    
    //绑定ip,端口
    svr.listen("0.0.0.0",atoi(argv[1]));


    // Json::Value value;
    // value["code"] = "#include<iostream>\n \
    // using namespace std;\n \
    // int main()\n \
    // {\n\
    // // 内存超限,返回6号信号\n\
    // //    while(true){char* ch = new char[1024*1024];}\n\
    // // 时间超限,返回24号信号\n\
    // //    while(true){};\n\
    //     cout << \"hello world\" << endl;\n\
    //     return 0;\n\
    // }";
    // value["cpu_limit"] = 1;
    // value["mem_limit"] = 3*10240;
    // Json::StyledWriter writer;
    // std::string in_json = writer.write(value);
    // std::cout << in_json << in_json.size() << "\n";
    // std::string out_json;
    // compile_run::Star(in_json,&out_json);

    return 0;
}